﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;

namespace E01
{
    /// <summary>
    /// 同步采购订货单
    /// </summary>
    public class A34 : ISYNChildSet
    {
        public void Code(ISYN handler, CodeResult result, SYNRight user, SerialType st, String parameter)
        {
            E01A34P P = (E01A34P)handler.Serial.Deserialize(st, typeof(E01A34P), parameter);

            try
            {
                SqlParameter[] sps = new SqlParameter[30];
                sps[0] = new SqlParameter("@ID", SqlDbType.NVarChar, 50);
                sps[0].Value = P.ID;
                sps[1] = new SqlParameter("@Number", SqlDbType.NVarChar, 50);
                sps[1].Value = P.Number;
                sps[2] = new SqlParameter("@BuildDate", SqlDbType.DateTime);
                sps[2].Value = Convert.ToDateTime(P.BuildDate);
                sps[3] = new SqlParameter("@CusName", SqlDbType.NVarChar, 50);
                sps[3].Value = P.CusName;
                sps[4] = new SqlParameter("@PurchItem", SqlDbType.NVarChar, 50);
                sps[4].Value = P.PurchItem;
                sps[5] = new SqlParameter("@BagStandard", SqlDbType.NVarChar, 50);
                sps[5].Value = P.BagStandard;
                sps[6] = new SqlParameter("@CalPlan", SqlDbType.NVarChar, 50);
                sps[6].Value = P.CalPlan;
                sps[7] = new SqlParameter("@Num", SqlDbType.Decimal);
                sps[7].Value = P.Num;
                sps[8] = new SqlParameter("@Price", SqlDbType.Decimal);
                sps[8].Value = P.Price;
                sps[9] = new SqlParameter("@TaxRate", SqlDbType.Decimal);
                sps[9].Value = P.TaxRate;
                sps[10] = new SqlParameter("@TaxPrice", SqlDbType.Decimal);
                sps[10].Value = P.TaxPrice;
                sps[11] = new SqlParameter("@TaxAmount", SqlDbType.Decimal);
                sps[11].Value = P.TaxAmount;
                sps[12] = new SqlParameter("@TotalAmount", SqlDbType.Decimal);
                sps[12].Value = P.TotalAmount;
                sps[13] = new SqlParameter("@UsedNum", SqlDbType.Decimal);
                sps[13].Value = P.UsedNum;
                sps[14] = new SqlParameter("@UsedAmount", SqlDbType.Decimal);
                sps[14].Value = P.UsedAmount;
                sps[15] = new SqlParameter("@TransPrice", SqlDbType.Decimal);
                sps[15].Value = P.TransPrice;
                sps[16] = new SqlParameter("@TransTaxRate", SqlDbType.Decimal);
                sps[16].Value = P.TransTaxRate;
                sps[17] = new SqlParameter("@TaxTransPrice", SqlDbType.Decimal);
                sps[17].Value = P.TaxTransPrice;
                sps[18] = new SqlParameter("@TransAmount", SqlDbType.Decimal);
                sps[18].Value = P.TransAmount;
                sps[19] = new SqlParameter("@Allowance", SqlDbType.Decimal);
                sps[19].Value = P.Allowance;
                sps[20] = new SqlParameter("@PurchMode", SqlDbType.Int);
                sps[20].Value = P.PurchMode;
                sps[21] = new SqlParameter("@OtherPrice", SqlDbType.Decimal);
                sps[21].Value = P.OtherPrice;
                sps[22] = new SqlParameter("@OtherAmount", SqlDbType.Decimal);
                sps[22].Value = P.OtherAmount;
                sps[23] = new SqlParameter("@IsForbidUse", SqlDbType.Bit);
                sps[23].Value = P.IsForbidUse;
                sps[24] = new SqlParameter("@IsForbidBalance", SqlDbType.Bit);
                sps[24].Value = P.IsForbidBalance;
                sps[25] = new SqlParameter("@MultiCheckStatus", SqlDbType.Int);
                sps[25].Value = P.MultiCheckStatus;
                sps[26] = new SqlParameter("@Remark", SqlDbType.NVarChar, 50);
                sps[26].Value = P.Remark;
                sps[27] = new SqlParameter("@BuysNum", SqlDbType.Decimal);
                sps[27].Value = P.BuysNum;
                sps[28] = new SqlParameter("@BuysAmount", SqlDbType.Decimal);
                sps[28].Value = P.BuysAmount;
                sps[29] = new SqlParameter("@SaleMode", SqlDbType.Int);
                sps[29].Value = P.SaleMode;
                

                StringBuilder sql = new StringBuilder();
                sql.Append("SET NOCOUNT ON\r\n");

                sql.Append("DECLARE @SupNameFID INT\r\n SET @SupNameFID = IsNull((SELECT FItemID FROM t_Supplier WHERE FHomePage =@CusName AND @CusName<>''),0)\r\n");
                sql.Append("DECLARE @PurchItemFID INT\r\n SET @PurchItemFID = IsNull((SELECT FID FROM KEW_PurchItemEntry WHERE FText =@PurchItem AND @PurchItem<>''),0)\r\n");
                sql.Append("DECLARE @CalPlanFID INT\r\n SET @CalPlanFID = IsNull((SELECT FID FROM KEW_PurchConfigEntry WHERE FText =@CalPlan AND @CalPlan<>''),0)\r\n");

                sql.Append("DECLARE @FID INT\r\n SET @FID=IsNull((SELECT 1 FROM KEW_PurchOrderQty WHERE FBillNo=@Number),0)");
                sql.Append("IF @FID>0 BEGIN\r\n");
                sql.Append("  UPDATE KEW_PurchOrderQty SET FDate=@BuildDate,FBase=@SupNameFID,FBase4=@PurchItemFID,FBase5=@CalPlanFID,FDecimal=@Num,FDecimal1=@Price,FDecimal2=@TaxRate,");
                sql.Append("  FDecimal3=@TaxPrice,FDecimal4=@TaxAmount,FDecimal9=@TotalAmount,FDecimal10=@BuysNum,FDecimal12=@BuysAmount, \r\n");
                sql.Append("  FDecimal5=@TransPrice,FDecimal6=@TransTaxRate,FDecimal7=@TaxTransPrice,FDecimal8=@TransAmount,FDecimal11=@Allowance,FComboBox1=@PurchMode,FDecimal13=@OtherPrice,FDecimal14=@OtherAmount,FMultiCheckStatus=@MultiCheckStatus, FCheckBox=@IsForbidUse,FCheckBox1=@IsForbidBalance,FNOTE=@Remark WHERE FBillNo=@Number\r\n");
                sql.Append("  SELECT 'U',@FID\r\n");
                sql.Append("END ELSE BEGIN\r\n");
                sql.Append("  EXEC GetICMaxNum 'KEW_PurchOrderQty',@FID OUTPUT\r\n");
                sql.Append("  INSERT INTO KEW_PurchOrderQty ([FClassTypeID],[FID],[FBillNo],[FDate],[FBase],[FBase4],[FBase5],[FDecimal],[FDecimal1],[FDecimal2], [FDecimal3],[FDecimal4],[FDecimal9],[FDecimal10],[FDecimal12],[FDecimal5],[FDecimal6],[FDecimal7],[FDecimal8],[FDecimal11],[FComboBox1],[FDecimal13],[FDecimal14],[FMultiCheckStatus],[FCheckBox],[FCheckBox1],[FNOTE],[FText]) ");
                sql.Append("  VALUES (236900113,@FID,@Number,@BuildDate,@SupNameFID,@PurchItemFID,@CalPlanFID,@Num,@Price,@TaxRate, @TaxPrice,@TaxAmount,@TotalAmount,@UsedNum,@UsedAmount,@TransPrice,@TransTaxRate,@TaxTransPrice,@TransAmount,@Allowance,@SaleMode,@OtherPrice,@OtherAmount,@MultiCheckStatus,@IsForbidUse,@IsForbidBalance,@Remark,@ID)\r\n");
                sql.Append("  SELECT 'I',@FID\r\n");
                sql.Append("END \r\n");
                sql.Append("SET NOCOUNT OFF\r\n");
                Recordset rs = handler.Data.ExeProcedure(P.BaseNo, sql.ToString(), sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows[0][0].ToString() == "U")
                    {
                        result.Result = EnumResult.Update;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                    }
                    else
                    {
                        result.Result = EnumResult.Insert;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                    }

                    result.Message = String.Format("采购订货单同步完成:{0},{1}", P.Number, result.Result.ToString());
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("采购订货单同步失败:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("采购订货单同步失败:{0}", ex.Message);
            }
        }

    }

    /// <summary>
    /// 采购订货单
    /// </summary>
    public class E01A34P
    {
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 三方内码
        /// </summary>
        public String ID;
        /// <summary>
        /// 三方编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 建档日期
        /// </summary>
        public String BuildDate;
        /// <summary>
        /// 客户名称
        /// </summary>
        public String CusName;
        /// <summary>
        /// 大宗原料
        /// </summary>
        public String PurchItem;
        /// <summary>
        /// 袋装标准
        /// </summary>
        public String BagStandard;
        /// <summary>
        /// 计量方案
        /// </summary>
        public String CalPlan;
        /// <summary>
        /// 数量
        /// </summary>
        public Decimal Num;
        /// <summary>
        /// 单价
        /// </summary>
        public Decimal Price;
        /// <summary>
        /// 税率
        /// </summary>
        public Decimal TaxRate;
        /// <summary>
        /// 含税单价
        /// </summary>
        public Decimal TaxPrice;
        /// <summary>
        ///含税金额
        /// </summary>
        public Decimal TaxAmount;
        /// <summary>
        /// 总金额
        /// </summary>
        public Decimal TotalAmount;
        /// <summary>
        /// 已用数量
        /// </summary>
        public Decimal UsedNum;
        /// <summary>
        /// 已用金额
        /// </summary>
        public Decimal UsedAmount;
        /// <summary>
        /// 购进数量
        /// </summary>
        public Decimal BuysNum;
        /// <summary>
        /// 购进金额
        /// </summary>
        public Decimal BuysAmount;
        /// <summary>
        ///运输单价
        /// </summary>
        public Decimal TransPrice;
        /// <summary>
        /// 运输税率
        /// </summary>
        public Decimal TransTaxRate;
        /// <summary>
        /// 含税运价
        /// </summary>
        public Decimal TaxTransPrice;
        /// <summary>
        /// 运输金额
        /// </summary>
        public Decimal TransAmount;
        /// <summary>
        /// 余量
        /// </summary>
        public Decimal Allowance;
        /// <summary>
        /// 采购方式
        /// </summary>
        public int PurchMode;
        /// <summary>
        /// 其他单价
        /// </summary>
        public Decimal OtherPrice;
        /// <summary>
        /// 其他余额
        /// </summary>
        public Decimal OtherAmount;
        /// <summary>
        /// 是否禁用使用
        /// </summary>
        public Boolean IsForbidUse;
        /// <summary>
        /// 是否禁用结算
        /// </summary>
        public Boolean IsForbidBalance;
        /// <summary>
        /// 审批流程状态（）
        /// </summary>
        public int MultiCheckStatus;
        /// <summary>
        /// 备注
        /// </summary>
        public String Remark;
        /// <summary>
        /// 采购方式
        /// </summary>
        public int SaleMode;

    }
}
